package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.mapper.SupplierMapper;
import com.gxa.pojo.Supplier;
import com.gxa.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 周林森
 * Date: 2021/12/06 20:23:06
 * Description: 供应商相关业务
 * Version: V1.0
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired(required = false)
    private SupplierMapper supplierMapper;

    /**
     * 添加供应商相关信息
     * @param map
     * @return
     */
    @Override
    public R addSupplier(Map map) {
        Map newsupplier = (Map)map.get("newsupplier");
        //查询新增供应商id是否已经存在
        String customerId = supplierMapper.querySupplierIdExist(newsupplier.get("supplier_Id"));
        if(customerId != null) {
            return new R(500,"添加失败");
        }
        //添加供应商基本信息
        supplierMapper.addSupplierInfo(newsupplier);
        //添加供应商联系人信息
        List<Map<String,Object>> mapList = (List<Map<String, Object>>) newsupplier.get("supplier_contact_peroson_infos");
        if (mapList!=null){
            for (Map<String,Object> map1 : mapList){
                map1.put("supplier_Id",newsupplier.get("supplier_Id"));
                supplierMapper.addSupplierContactPeopleInfo(map1);
            }
        }
        return new R(200,"添加成功");
    }

    /**
     * 修改供应商相关信息
     * @param map
     * @return
     */
    @Override
    public R updateSupplier(Map map) {
        Map newsupplier = (Map)map.get("newsupplier");
        //修改供应商基本信息
        int i1 = supplierMapper.updateSupplierBaseInfo(newsupplier);
        //查询供应商联系人记录编号
        List<Integer> list = supplierMapper.querySupContactId(newsupplier.get("supplier_Id"));
        //修改供应商联系人信息
        List<Map<String,Object>> mapList = (List<Map<String, Object>>) newsupplier.get("supplier_contact_peroson_infos");
        if(mapList.size() < list.size()){
            for (Map<String,Object> contactPerosonInfo: mapList) {
                Integer id = (Integer) contactPerosonInfo.get("contact_Person_Id");
                list.remove(id);
            }
            Integer[] array1 = new Integer[list.size()];
            for (int i =0; i < list.size(); i++){
                array1[i]=list.get(i);
            }
            supplierMapper.delContactById(array1,newsupplier.get("supplier_Id"));
        }else if(mapList.size() > list.size()){
            for (Map<String,Object> contactPerosonInfo: mapList) {
                Object id = contactPerosonInfo.get("contact_Person_Id");
                if(!(list.contains(id))){
                    contactPerosonInfo.put("supplier_Id",newsupplier.get("supplier_Id"));
                    supplierMapper.addSupplierContactPeopleInfo(contactPerosonInfo);
                }
            }
        }
        for (Map<String,Object> contactPerosonInfo: mapList) {
            supplierMapper.updateSupplierContactInfo(contactPerosonInfo);
        }
        return new R(200,"修改客户信息成功");
    }

    /**
     * 查询供应商
     * @param map
     * @return
     */
    @Override
    public R querySupplier(Map map) {
        List<Supplier> supplierList = supplierMapper.querySupplierList(map);
        return new R(200,"查询供应商成功",supplierList);
    }

    /**
     * 删除供应商
     * @param supplier_id
     * @return
     */
    @Override
    public R delSupplier(Object supplier_id) {
        supplierMapper.delSupplierById(supplier_id);
        return new R(200,"删除成功");
    }

    /**
     * 修改供应商状态
     * @param supplier_id
     * @param status
     * @return
     */
    @Override
    public R updateSupplierStatus(Object supplier_id, Object status) {
        String param = "true";
        if("true".equals((String)status)){
            param = "false";
        }
        int i = supplierMapper.updateSupplierStatus(param,supplier_id);
        if (i>0){
            return new R(200,"修改状态成功");
        }
        return new R(200,"修改状态失败");
    }


}
